﻿Imports System.IO
Imports NENAData.Email

Partial Public Class AdminEmailMembers1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack Then
            Exit Sub
        End If

        Dim intAffiliateId As Integer
        intAffiliateId = CType(Request.QueryString("AffiliateID"), Integer)
        If intAffiliateId > 0 Then
            ddlAffiliates.SelectedValue = intAffiliateId
            ddlAffiliates.Enabled = False
        End If
        'Select Case intAffiliateId
        '    Case 2
        '        ddlAffiliates.SelectedValue = intAffiliateId
        'End Select
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
        'Response.Write("Affiliate ID: " + CType(ddlAffiliates.SelectedValue, Integer).ToString)
        'Response.Write("<br />Transaction ID: " + CType(ddlTransactionType.SelectedValue, Integer).ToString)

        Dim strAttachmentFileName1 As String = String.Empty
        Dim strAttachmentFileName2 As String = String.Empty

        If ofdAttachment1.PostedFile.FileName <> "" Then
            strAttachmentFileName1 = Server.MapPath(UploadAttachment(ofdAttachment1.PostedFile))
        End If
        If ofdAttachment2.PostedFile.FileName <> "" Then
            strAttachmentFileName2 = Server.MapPath(UploadAttachment(ofdAttachment2.PostedFile))
        End If
        NENAData.Email.Attachment.Newsletter(CType(ddlAffiliates.SelectedValue, Integer), _
                                             CType(ddlTransactionType.SelectedValue, Integer), _
                                             txtFrom.Text, txtSubject.Text, ftbBody.Text, _
                                            strAttachmentFileName1, strAttachmentFileName2)
        Response.Redirect("http://membership.nena.ca/secure/", False)
    End Sub

    Private Function UploadAttachment(ByVal pHttpPostedFile As HttpPostedFile) As String
        Dim strFileName, strUploadTime, strOnlyFileName As String
        Dim strAttachFullFileName As String = String.Empty

        Try

            'Get a reference to PostedFile object
            Dim ulFile As HttpPostedFile = pHttpPostedFile
            'Get size of the file
            Dim nFileLen As Integer = ulFile.ContentLength
            'Make sure the size of the file is > 0
            If nFileLen > 0 Then
                'Get the file name
                strFileName = Path.GetFileName(pHttpPostedFile.FileName)
                'Preced the file name with "attachments/" so 
                strUploadTime = "-" & Now.Day & Now.Month & Now.Year & "." & Now.Hour & Now.Minute
                strOnlyFileName = Left(strFileName, strFileName.Length - 4) & strUploadTime & Right(strFileName, 4)
                'strUserAttachmentMessage = strOnlyFileName
                'the file is saved to our attachments directory
                strAttachFullFileName = "Attachments/" & strOnlyFileName
                'Save the file on the server
                pHttpPostedFile.SaveAs(Server.MapPath(strAttachFullFileName))
            End If
        Catch ex As Exception
            'upload failed
        End Try
        Return strAttachFullFileName
    End Function

End Class